package com.imooc.animation360.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

public class MyProgressView2 extends View {
	private int width=150;
	private int height=150;
	private Paint circlePaint;
	private Paint progressPaint;
	private Paint textPaint;
	//空的Bitmap,本例是先将所有元素全部都绘制在这个空的Bitmap上,然后将这个Bitmap绘制到控件上.
	private Bitmap bitmap;
	//与上面的空Bitmap相关联的画布.
	private Canvas bitmapCanvas;
	//绘制波浪线与底部的闭合区域的路径对象.
	private Path path;
	private int progress = 75;
	private int max = 150;
	private String progressText;

	public MyProgressView2(Context context) {
		this(context,null);
	}

	public MyProgressView2(Context context, AttributeSet attrs) {
		this(context, attrs,0);
	}

	public MyProgressView2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	
	private void init(){
		//这个判断必须有,否则混合效果体现不出来.
		if(Build.VERSION.SDK_INT >= 11){
            setLayerType(LAYER_TYPE_SOFTWARE, null);
        }
		
		circlePaint = new Paint();
		circlePaint.setColor(Color.argb(0xff, 0x3a, 0x8c, 0x6c));
		circlePaint.setAntiAlias(true);
		
		progressPaint = new Paint();
		progressPaint.setColor(Color.argb(0xff, 0x4e, 0xc9, 0x63));
		progressPaint.setAntiAlias(true);
		progressPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); //只绘制重叠的部分.
		path = new Path();
		
		textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setAntiAlias(true);
		textPaint.setTextSize(25);
		
		//新建一个空的透明的画布.
//		bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
//		bitmapCanvas = new Canvas(bitmap);
	}
	
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(width, height);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		//1.新建图层.
        int layerId = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
			//将圆形绘制在画布上.
        	canvas.drawCircle(width/2, height/2, width/2, circlePaint);
			
			//绘制波浪线与圆圈下部的闭合区域,需要使用贝塞尔曲线.
			path.reset();  //reset一定要有,否则会出现诡异的黑色,设置什么混合模式都不管用.
			float y = (1-(float)progress/max)*height;  //右上角的点
			path.moveTo(width, y);
			path.lineTo(width, height);
			path.lineTo(0, height);
			path.lineTo(0, y);
			//循环一次绘制贝塞尔曲线宽度共计40像素,所以4次可以绘制完,又由于progressPaint的mode设置为只绘制叠加部分,故超出宽度也没关系.
			for(int i=0;i<4;i++){
				path.rQuadTo(10, -10, 20, 0);
				path.rQuadTo(10, 10, 20, 0);
			}
			path.close();
			canvas.drawPath(path, progressPaint);
			 
			//绘制文本.
			progressText = (int)(((float)progress/max)*100) + "%";
			float textWidth = textPaint.measureText(progressText);
			float baseLinex = width/2-textWidth/2;
			FontMetrics fontMetrics = textPaint.getFontMetrics();
			float dy = -(fontMetrics.ascent+fontMetrics.descent)/2;
			float baseLiney = height/2+dy;
			canvas.drawText(progressText, baseLinex, baseLiney, textPaint);
			
//			//将bitmap绘制出来.
//			canvas.drawBitmap(bitmap, 0, 0, null);
        
		//2.保存该图层.
        canvas.restoreToCount(layerId);
	}
}
